package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_see_his;

import cn.com.api.service.ApiU_see_hisService;
import cn.com.api.service.CheckService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "see_his/")
public class ApiU_see_hisController {
    @Autowired
    private ApiU_see_hisService apiU_see_hisService;
    @Autowired
	private CheckService checkService;
	
	private static Logger logger = Logger.getLogger(ApiU_see_hisController.class);
	
	/**
	 * 谁看过我
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "getU_see_his")
	public void getU_see_his(HttpServletRequest request, HttpServletResponse response, 
			String user_id, String token, PageInfo pageInfo) throws Exception {
        
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,String> params = new HashMap<String,String>();
		params.put("see_uid", user_id);
		
		try{
			if (checkService.checkToken(user_id, token, map) ) {
				List<ApiU_see_his> see_List = this.apiU_see_hisService.findU_see_hissByMapListPage(params, pageInfo);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功");
				map.put(Utils.DATAS, see_List);
			}	
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
    		map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 我看谁
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "seeWho")
	public void seeWho(HttpServletRequest request, HttpServletResponse response, 
			String user_id, String token, String see_uid) throws Exception {
        
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,String> params = new HashMap<String,String>();
		params.put("user_id", user_id);
		params.put("see_uid", see_uid);
		
		PageInfo pageInfo = new PageInfo();
		pageInfo.setShowCount(10);
		pageInfo.setCurrentPage(1);
		
		try{
			if (checkService.checkToken(user_id, token, map) ) {
				
				List<ApiU_see_his> see_List = this.apiU_see_hisService.findU_see_hissByMapListPage(params, pageInfo);
				if(see_List == null || see_List.size() == 0 ){
					ApiU_see_his see_his = new ApiU_see_his();
					see_his.setUser_id(user_id);
					see_his.setSee_uid(see_uid);
					see_his.setSee_time(Util.CurrentTime());
					see_his.setIs_read(0);
					this.apiU_see_hisService.U_see_hisInsertion(see_his);
				}else {
					ApiU_see_his see_his = see_List.get(0);
					see_his.setSee_time(Util.CurrentTime());
					this.apiU_see_hisService.updateU_see_his(see_his);
				}
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}	
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
    		map.put(Utils.DATAS, null);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
}


